var parkChargeCollectJs = function (targetObj) {
	this.targetObj = targetObj;
	this.sysUtils = new kendoUtils();
	this.init();
	this.dateFormat = "yyyy-MM-dd HH:mm:ss";
	return this;
};

parkChargeCollectJs.prototype = {
		init : function () {
			var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj.find('.search-panel'));
			var functionArray = [
			                     {fName : 'search_btn' , fs : this.findReport},
			                     {fName : 'reset_btn' , fs : this.resetInfo}
			                     ];
			this.models = new bindBaseModel(null, fieldArray, functionArray, null);
			this.sysUtils.bindDoucment(this.models, this.targetObj, this);
			this.initHtml();
			//默认查最近一个月的数据
			var date = new Date();
			date.setHours(8, 0, 0, 0);
			this.sysUtils.mvvmModel.set("endTime",kendo.toString(date,this.dateFormat));
			var bgTime = new Date();
			bgTime.setHours(8, 0, 0, 0);
			bgTime.setDate(bgTime.getDate() - 1);
			this.sysUtils.mvvmModel.set("beginTime",kendo.	toString(bgTime,this.dateFormat));
			Utils.createDataDictDropDownList([{div:this.targetObj.find('#chargeMethod'), parentModelKey:BASIS.CHARGE_METHOD.ROOT},
			                                  {div:this.targetObj.find('#payPlatform'), parentModelKey:BASIS.PAY_PLATFORM.ROOT}], this);
			//计算显示区域高度
			this.targetObj.find("#parkChargeCollectReport").height(this.targetObj.height() - this.targetObj.find(".search-panel").outerHeight());
			
		},
		initHtml:function(){
			this.userInfo = null;
			var that = this;
			var autoFlag = false;
			var selectFun = function(e){
				that.userInfo = this.dataItem(e.item);
				autoFlag = true;
			};
			var changeFun = function(e){
				if(!autoFlag){
					that.userInfo = null;
					that.targetObj.find("#cashierId").val("");
				}
				autoFlag = false;
			};
			this.sysUtils.autoCompleteUser("cashierId", "userName",selectFun,changeFun);
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.models.bindFieldArray);
			that.userInfo = null;
			that.targetObj.find("#cashierId").val("");
			//默认查最近一个月的数据
			var date = new Date();
			that.sysUtils.mvvmModel.set("endTime",kendo.toString(date,that.dateFormat));
			date.setMonth(date.getMonth() - 1);
			that.sysUtils.mvvmModel.set("beginTime",kendo.toString(date,that.dateFormat));
		},
		/**
		 * 获取查询参数
		 * @param e
		 */
		getQueryParams:function(params){
			if(isEmpty(params["beginTime"]) || isEmpty(params["endTime"])){
				notification({title : '提示', message : '开始时间和结束时间为必填，请选择！！'}, 'info');
				return;
			}
			params["beginTime"] = kendo.toString(params["beginTime"],this.dateFormat);
			params["endTime"] = kendo.toString(params["endTime"],this.dateFormat);
			//检查查询时间段是否超过半年
			var beginTime = kendo.parseDate(params["beginTime"],this.dateFormat);
			var endTime =  kendo.parseDate(params["endTime"],this.dateFormat);
			beginTime.setMonth(beginTime.getMonth() + 12);
			if(endTime > beginTime){
				notification({title : '提示', message : '开始时间和结束时间相差不能超过一年！！'}, 'info');
				return;
			}
			if(null != this.userInfo && !isEmpty(this.targetObj.find("#cashierId").val())){
				params["cashierId"] =  this.userInfo.id;
			}
			return params;
		},
		/**
		 * 查询报表数据
		 * @param e
		 */
		findReport:function(e){
			var that = this;
			if(null != e){
				that = $(e.target).data("that");
			}
			that.targetObj.find("#reportTable").find("tr:not(:first)").empty();
			that.targetObj.find("#timeReport").html("");
			that.targetObj.find("#totalMoney").html("");
			var params = that.sysUtils.getBindDocJson(that.models.bindFieldArray);
			params = that.getQueryParams(params);
			JsonpAjax(baseApiPath + 'report/chargeCollect', JSON.stringify(params), 'json', function (maps) {
				if(isEmpty(maps)){
					notification({title : '提示', message : '此时间段无任何统计数据！！'}, 'info');
					return;
				}
				var payPlatformMaps = maps;
				var reportTable = that.targetObj.find("#reportTable");
				var totalMoney = 0;
				var deuTotalMoney = 0;
				var preTotalMoney = 0;
				//第一层支付平台
				for(var field in payPlatformMaps){
					var chargeMaps = payPlatformMaps[field];
					if(null == chargeMaps) continue;
					var payPlatCount = 0;
					var platTd = $("<td>"+field+"</td>");
					var cmonrySum = 0;
					var deuMoneySum = 0;
					var preMoneySum = 0;
					//第二层收费人
					for(var fieldtt in chargeMaps){
						var userCmonrySum = 0;
						var userDeuMoneySum = 0;
						var userpreMoneySum = 0;
						var chargeList = chargeMaps[fieldtt];
						if(null == chargeList) continue;
						var chargTd = $("<td>"+(fieldtt.indexOf("null")>=0?"==车主自缴==":fieldtt)+"</td>");
						var chargeCount = 0;
						//实际收费信息
						for(var i = 0; i < chargeList.length; i++){
							var gridTr = $("<tr></tr>");
							if(payPlatCount++ ==0){
								gridTr.append(platTd);
							}
							if(chargeCount++ == 0){
								gridTr.append(chargTd);
							}
							gridTr.append("<td>"+chargeList[i]["chargeMethodName"]+"</td>");
							gridTr.append("<td>"+(chargeList[i]["chargeMoney"]).toFixed(2)+"</td>");
							gridTr.append("<td>"+(chargeList[i]["dueMoney"]).toFixed(2)+"</td>");
							gridTr.append("<td>"+(chargeList[i]["preMoney"]).toFixed(2)+"</td>");
							reportTable.append(gridTr);
							userCmonrySum += chargeList[i]["chargeMoney"];
							userDeuMoneySum += chargeList[i]["dueMoney"];
							userpreMoneySum += chargeList[i]["preMoney"];
						}
						cmonrySum += userCmonrySum;
						deuMoneySum += userDeuMoneySum;
						preMoneySum += userpreMoneySum;
						reportTable.append("<tr><td style='text-align: right;'>小计</td><td>"+userCmonrySum.toFixed(2)+"</td>" +
								"<td>"+userDeuMoneySum.toFixed(2)+"</td><td>"+userpreMoneySum.toFixed(2)+"</td></tr>");
						chargTd.attr("rowspan",chargeCount + 1);
						payPlatCount++;
					}
					reportTable.append("<tr><td colspan=2 style='text-align: right;'>小计</td><td>"+cmonrySum.toFixed(2)+"</td>" +
							"<td>"+deuMoneySum.toFixed(2)+"</td><td>"+preMoneySum.toFixed(2)+"</td></tr>");
					platTd.attr("rowspan",payPlatCount + 1);
					totalMoney += cmonrySum;
					deuTotalMoney += deuMoneySum;
				    preTotalMoney += preMoneySum;
				}
				reportTable.append("<tr><td colspan=3 style='text-align: right;'>合计</td><td>"+totalMoney.toFixed(2)+"</td>" +
						"<td>"+deuTotalMoney.toFixed(2)+"</td><td>"+preTotalMoney.toFixed(2)+"</td></tr>");
				//改变title信息
				that.targetObj.find("#timeReport").html(params["beginTime"] +" - " +params["endTime"] + " ");
				that.targetObj.find("#totalMoney").html(totalMoney.toFixed(2));
			}, true);
		},
		/**
		 * 打印
		 * @param e
		 */
		print:function(e){
			this.targetObj.find("#parkChargeCollectReport").find("div:first").printArea({
				mode:"popup",
				popTitle:"",
				popClose:true,
				popHt:600,
				popWd:1000
			});
		},
		/**
	     * 导出Excel文件
	     */
	    exportExcel:function(){
	    	 var params = this.sysUtils.getBindDocJson(this.models.bindFieldArray);
	    	 params = this.getQueryParams(params);
	         window.open(baseApiPath+'report/exportCollectExcel?jsonData='+encodeURI(JSON.stringify(params)));
	    }
};